/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.delivery.dao;

import com.mycompany.delivery.entities.Courier;
import com.mycompany.delivery.entities.Employee;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Martin Huněk
 */
@Repository(value = "EmployeeDao")
public class EmployeeDAOImpl implements EmployeeDAO {

    @PersistenceContext(unitName = "TestPU")
    private EntityManager manager;

    @Override
    public void createEmployee(Employee employee) {
        if (employee == null) {
            throw new IllegalArgumentException("employee is null");
        } else if (employee.getLogin() == null) {
            throw new IllegalArgumentException("employee login is null");
        }

        try {
            manager.persist(employee);
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
    }

    @Override
    public void updateEmployee(Employee employee) {
        if (employee == null) {
            throw new IllegalArgumentException("employee is null");
        } else if (employee.getLogin() == null) {
            throw new IllegalArgumentException("employee login is null");
        }
        try {
            manager.merge(employee);
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
    }

    @Override
    public void deleteEmployee(Employee employee) {
        if (employee == null) {
            throw new IllegalArgumentException("employee is null");
        } else if (employee.getId() == null) {
            throw new IllegalArgumentException("employee id is null");
        }

        try {
            manager.remove(manager.find(Employee.class, employee.getId()));
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
    }

    @Override
    public Employee getEmployeeById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("id is null");
        }

        Employee employee;
        try {
            employee = (Employee) manager.find(Employee.class, id);
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return employee;
    }

    @Override
    public Employee getEmployeeByLogin(String login) {
        if (login == null) {
            throw new IllegalArgumentException("login is null");
        }
        List<Employee> employees;
        try {
            //getSingle result vrací excpetion a my chceme aby i když nic nenajde takto vrátilo null
            //    Find more than one object from the query result: NonUniqueResultException
            //    Find no result: NoResultException

            employees = (List<Employee>) manager.createQuery("SELECT c FROM Employee c WHERE c.login LIKE :cLogin ").setParameter("cLogin", login).getResultList();
            if (employees.size() != 1) {
                return null;
            } else {
                return employees.get(0);
            }
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
    }

    @Override
    public List<Employee> getAllEmployees() {
        List<Employee> employees;
        try {
            employees = manager.createQuery("SELECT c FROM Employee c").getResultList();
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return employees;
    }

    @Override
    public boolean isLoginUnique(String login) {
        if (login == null) {
            throw new IllegalArgumentException("login is null");
        }
        List<Courier> couriers;
        List<Employee> employees;
        try {
            couriers = (List<Courier>) manager.createQuery("SELECT c FROM Courier c WHERE c.login = :cLogin ").setParameter("cLogin", login).getResultList();
            employees = (List<Employee>) manager.createQuery("SELECT c FROM Employee c WHERE c.login = :cLogin ").setParameter("cLogin", login).getResultList();

        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return (employees.isEmpty() && couriers.isEmpty());
    }

}
